/**
 * The UOM main menu bar
 * @author  zulianp@lu.unisi.ch & lemmar@lu.unisi.ch
 * @version 0.1
 */

package ch.unisi.inf.projects.java.uom.gui; 

import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import ch.unisi.inf.projects.java.uom.elements.*;
import ch.unisi.inf.projects.java.uom.gui.commands.*; 

public final class UOMMenuBar extends JMenuBar implements ItemManagerListener
{
    /** We are not going to serialize this class atm */
    private static final long serialVersionUID = -1L;

    private final List<JMenuItem> closedItems;
    
    private final List<JMenuItem> openedItems;
    
    public UOMMenuBar()
    {
    	closedItems = new ArrayList<JMenuItem>();
    	openedItems = new ArrayList<JMenuItem>();
    
        //file menu
        final JMenu fileMenu = new JMenu("File");
        fileMenu.add(new QuitCommand());
        this.add(fileMenu);
        
        // Database menu
        final JMenu dbMenu = new JMenu("Database");
        final JMenuItem menuDBCreate = new JMenuItem(new CreateDatabaseCommand());
        openedItems.add(menuDBCreate);
        final JMenuItem menuDBOpen = new JMenuItem(new OpenDatabaseCommand());

        openedItems.add(menuDBOpen);
        final JMenuItem menuDBClose = new JMenuItem(new CloseDatabaseCommand());
        closedItems.add(menuDBClose);
        dbMenu.add(menuDBCreate);
        dbMenu.addSeparator();
        dbMenu.add(menuDBOpen);
        dbMenu.add(menuDBClose);
        this.add(dbMenu);
        
        //HelpMenu
        final JMenu helpMenu = new JMenu("Help");
        final JMenuItem menuItemAbout = new JMenuItem(new OpenAboutDialogCommand());
        helpMenu.add(menuItemAbout);
        this.add(helpMenu);
        
        setStateToItems(ItemManager.instance().databaseIsOpen());
        
        ItemManager.instance().addItemManagerListener(this);
    }

    private void setStateToItems (final boolean state)
    {
    	for (final JMenuItem menuItem : closedItems)
    		menuItem.setEnabled(state);
    	for (final JMenuItem menuItem : openedItems)
    		menuItem.setEnabled(!state);
    }
    
	public void databaseClosed () { setStateToItems(false); }
	public void databaseOpened () { setStateToItems(true); }
	public void elementDeleted (final Element e) { return; }
	public void elementModified (final Element e) { return; }
	public void focusOnItem (final Item i) { return; }
	public void itemAdded (final Item i) { return; }
	public void itemClassAdded (final ItemClass itemClass) { return; }

}
